﻿using System;
using System.Web.Mvc;
using Framework.Domain;
using Framework.Mvc.ViewModels;
using Shell.DomainDot;
using Framework.Poco;

namespace Framework.Mvc.Controllers
{
    public class AgencyArticleController : Controller
    {
        private IDomainDotFactory factory;

        public AgencyArticleController()
        {
            this.factory = DomainDotFactory.Factory;
        }

        /// <summary>
        /// 属于经销商的文章列表
        /// </summary>
        public ActionResult List(Agency currentAgency, int currentPage = 1)
        {
            if (currentAgency != null)
            {
                var domain = this.factory.Create<SelectAgencyArticleByAgency>(currentAgency.Id);
                var total = 0;
                var articles = domain.Select(currentPage, Constans.PageSize, out total);
                var model = new AgencyArticleListViewModel();
                model.CurrentPage = currentPage;
                model.PageSize = Constans.PageSize;
                model.Total = total;
                model.List = articles;
                return View(model);
            }
            return this.RedirectToAction("LogOn", "Agency");
        }

        /// <summary>
        /// 经销商文章创建
        /// </summary>
        [HttpGet]
        public ActionResult Create(Agency currentAgency)
        {
            if (currentAgency != null)
            {
                return View();
            }
            return this.RedirectToAction("LogOn", "Agency");
        }

        /// <summary>
        /// 经销商文章创建
        /// </summary>
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(Agency currentAgency, AgencyArticleViewModel model)
        {
            if (currentAgency != null)
            {
                var context = factory.Create<CreateAgencyArticle>(currentAgency.Id);
                context.Create(model.Instance);
                return this.RedirectToAction("List");
            }
            return this.RedirectToAction("LogOn", "Agency");
        }

        /// <summary>
        /// 修改文章
        /// </summary>
        [HttpGet]
        public ActionResult Edit(Agency currentAgency, Guid articleId)
        {
            if (currentAgency != null)
            {
                var domain = this.factory.Create<SelectAgencyArticle>();
                var article = domain.Select(articleId);
                if (article == null)
                {
                    return RedirectToAction("List");
                }
                var model = new AgencyArticleViewModel();
                model.Instance = article;
                return View(model);
            }
            return this.RedirectToAction("LogOn", "Agency");
        }

        /// <summary>
        /// 修改文章
        /// </summary> 
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(Agency currentAgency, AgencyArticleViewModel model)
        {
            if (currentAgency != null)
            {
                try
                {
                    var domain = this.factory.Create<UpdateAgencyArticle>(currentAgency.Id);
                    domain.Update(model.Instance);
                    return RedirectToAction("List");
                }
                catch
                {
                    return View();
                }
            }
            return this.RedirectToAction("LogOn", "Agency");
        }

        /// <summary>
        /// 文章删除
        /// </summary>
        [HttpGet]
        public ActionResult Delete(Agency currentAgency, Guid articleId)
        {
            if (currentAgency != null)
            {
                var domain = this.factory.Create<SelectAgencyArticle>();
                var article = domain.Select(articleId);
                if (article == null)
                {
                    return RedirectToAction("List");
                }
                var model = new AgencyArticleViewModel();
                model.Instance = article;
                return View(model);
            }
            return this.RedirectToAction("LogOn", "Agency");
        }

        /// <summary>
        /// 文章删除
        /// </summary>
        [HttpPost]
        public ActionResult Delete(Agency currentAgency, AgencyArticleViewModel model)
        {
            if (currentAgency != null)
            {
                try
                {
                    // TODO: Add delete logic here
                    var domain = factory.Create<DeleteAgencyArticle>(currentAgency.Id);
                    domain.Delete(model.Instance.Id);
                    return RedirectToAction("List");
                }
                catch
                {
                    return View();
                }
            }
            return this.RedirectToAction("LogOn", "Agency");
        }
    }
}
